package com.my.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.my.bean.*;
import com.my.mapper.RoleMapper;
import com.my.service.RoleMenuService;
import com.my.service.RoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.my.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-26
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RoleMenuService roleMenuService;
    @Autowired
    RoleUserService roleUserService;
    @Autowired
    RoleService roleService;
    @Override
    public ResultPage<Role> selectMyPage(VoPage voPage,Role role,Long areaid,boolean admin) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<Role> list = baseMapper.selectMyPage(paramMap,role,areaid,admin);
        Integer count = baseMapper.countMyPage(paramMap,role,areaid,admin);
         ResultPage<Role>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


    @Override
    @Transactional
    public boolean insertRoleAndMenu(String param) {
        //把传过来的json字符串转成Map集合
        Map mapObj = JSONObject.parseObject(param,Map.class);
        Role role=new Role();
        Object o = mapObj.get("areaId");
        long l = Long.valueOf(String.valueOf(o));
        //获取map中 填入的角色名字
        role.setRoleName(mapObj.get("roleName")+"");
        role.setAreaid(l);
        role.setAppoint(mapObj.get("appoint")+"");
        role.setCreateTime(new Date().getTime());

        System.out.println("role:"+role);
        //该角色唯一标识
        role.setRoleValue(UUID.randomUUID()+"");
        //如果新增角色成功
        if(this.insert(role)){
            //获取map中所有选中的菜单id
            String menuId=(String) mapObj.get("menuId");
            //分割成独立的id
            String[] strs=menuId.split(",");

            //循环遍历数组中的所有菜单ID
            for (int i = 0; i < strs.length; i++) {
                boolean flag=false;
                RoleMenu roleMenu=new RoleMenu();
                 roleMenu.setmId(Long.parseLong(strs[i]));
                roleMenu.setrId(role.getId());
                //保存角色和菜单关联表
                if( roleMenuService.insert(roleMenu)){
                    flag=true;
                    System.out.println("role2:"+role);
                    //roleService.updateRoleValue(appoint);
                } else{
                    flag=false;
                }
                //如果flag是假  就返回false
                if (!flag){
                    return  false;
                }
            }
        }
        return  true;

    }

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public String deleteRole(Long id) throws SQLException {
     Role role=new Role();
        role.setId(id);
        if(this.deleteById(role)){
            Map map=new HashMap<>();
            map.put("role_id",role.getId());
          roleUserService.deleteByMap(map);
                Map map1=new HashMap<>();
                map1.put("r_id",role.getId());
               if(roleMenuService.deleteByMap(map1)){
                   return  "删除成功";
               };


        }
        throw   new  SQLException();
    }


    //指定人可以添加房间
    @Override
    public boolean updateRoleValue() {
        return baseMapper.updateRoleValue();
    }
}
